from datetime import datetime
from django.db import models

class Exam(models.Model):
    name = models.CharField(max_length=200)
    owner = models.CharField(max_length=200)
    created = models.DateTimeField(default=datetime.now())
    examStartDay = models.DateField()
    examEndDay = models.DateField()
    examStartTime = models.TimeField()
    examEndTime = models.TimeField()
    averageGrade = models.DecimalField(max_digits=4, decimal_places=3)
    duration = models.DecimalField(max_digits=5, decimal_places=2) #hours
    active = models.IntegerField(default='1')

class Question(models.Model):
    name = models.CharField(max_length=400)
    examId = models.ForeignKey(Exam)
    value = models.DecimalField(max_digits=3, decimal_places=0)
    order = models.IntegerField()
    correctAnswer = models.IntegerField()

class Answer(models.Model):
    name = models.CharField(max_length=400)
    rightAnswer = models.BooleanField()
    questionId = models.ForeignKey(Question)
    order = models.IntegerField()

class StudentExams(models.Model):
    studentId = models.IntegerField()
    examId = models.IntegerField()
    questionId = models.IntegerField()
    answerValue = models.IntegerField()

class Teacher(models.Model):
    username = models.CharField(max_length=100)

#http://stackoverflow.com/questions/507795/can-i-use-a-database-view-as-a-model-in-django
#http://stackoverflow.com/questions/1041855/use-database-view-in-django
class ResultByStudent(models.Model):
    studentId = models.IntegerField(db_column="studentId")
    examId = models.IntegerField(db_column="examid")
    name = models.CharField(max_length=200, db_column="name")
    grade = models.IntegerField(db_column="grade")
    class Meta:
        db_table = 'exams_resultbystudent'
        managed = False

class  ExamFinished(models.Model):
    studentId = models.IntegerField(db_column="studentId")
    examId = models.IntegerField(db_column="examid")
    name = models.CharField(max_length=200)
    examStartDay = models.DateField(db_column="examStartDay")
    examEndDay = models.DateField(db_column="examEndDay")
    class Meta:
        db_table = 'exams_examfinished'
        managed = False
